package com.toefl.speaking.exception;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;

import org.apache.http.HttpException;
import org.apache.http.client.ClientProtocolException;

import android.content.ActivityNotFoundException;
import android.database.sqlite.SQLiteFullException;
import android.os.Environment;

public class Logger {

	public static void printStackTrace(String TAG, Exception e) {
		logException(TAG, e);
	}

	public static void printStackTrace(String TAG, IOException e) {
		logException(TAG, e);
	}

	public static void printStackTrace(String TAG, ClientProtocolException e) {
		logException(TAG, e);
	}

	public static void printStackTrace(String TAG, MalformedURLException e) {
		logException(TAG, e);
	}

	/**
	 * 非法参数
	 * 
	 * @param e
	 */
	public static void printStackTrace(String TAG, IllegalArgumentException e) {
		logException(TAG, e);
	}

	public static void printStackTrace(String TAG, HttpException e) {
		logException(TAG, e);
	}

	public static void printStackTrace(String TAG, ActivityNotFoundException e) {
		logException(TAG, e);
	}

	public static void printStackTrace(String TAG, IndexOutOfBoundsException e) {
		logException(TAG, e);
	}

	/**
	 * 
	 * @param e
	 */
	public static void printStackTrace(String TAG, FileNotFoundException e) {
		logException(TAG, e);
	}

	// ~~~ 数据库相关

	public static void printStackTrace(String TAG, android.database.sqlite.SQLiteException e) {
		logException(TAG, e);
	}

	/**
	 * 数据库文件已达到最大空间(数据库已满)
	 * 
	 * @param e
	 */
	public static void printStackTrace(String TAG, SQLiteFullException e) {
		logException(TAG, e);
	}

	/**
	 * 未捕获的异常
	 * 
	 * @param TAG
	 * @param e
	 */
	public static void printStackTrace(String TAG, Throwable e) {
		logException(TAG, e);
	}

	/**
	 * 记录错误日志
	 * 
	 * @param TAG
	 * @param ex
	 */
	private static void logException(String TAG, Throwable ex) {
		/**
		 * if (NetworkUtil.isNetworkAvailable(CoreApplication.getInstance(). getBaseContext())) { //上传错误信息 if
		 * (!CoreApplication.getInstance().uploadErrorMessage (getErrorMessage(TAG, ex))) { //上传不成功 本地保存
		 * saveLogToLocal(TAG, ex); } } else { saveLogToLocal(TAG, ex); }
		 ***/

		StringBuffer sb = new StringBuffer();
		sb.append(ex.toString());
		StackTraceElement[] sta = ex.getStackTrace();
		for (int i = 0; i < sta.length; i++) {
			sb.append("\n");
			sb.append(sta[i].toString());
		}
		// DialogHelper.showToastException(AppConstants.ctx, TAG, sb.toString());

	}

	/**
	 * 获取错误信息
	 * 
	 * @param TAG
	 * @param ex
	 * @return
	 */
	public static String getErrorMessage(String TAG, Throwable ex) {
		String result = "";
		try {
			StringWriter sw = new StringWriter();
			PrintWriter printWriter = new PrintWriter(sw);
			// printWriter.append(StringUtil.getDateTime());
			printWriter.append("\tTAG:").append(TAG).append("\t\n");
			ex.printStackTrace(printWriter);

			Throwable cause = ex.getCause();
			while (cause != null) {
				cause.printStackTrace(printWriter);
				cause = cause.getCause();
			}
			printWriter.flush();
			result = sw.getBuffer().toString();
			printWriter.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 保存日志到本地SD卡
	 * 
	 * @param TAG
	 * @param ex
	 */
	// public static void saveLogToLocal(String TAG, Throwable ex) {
	// try {
	// PrintWriter printWriter = new PrintWriter(getLogFileName());
	// // printWriter.append(StringUtil.getDateTime());
	// printWriter.append("\tTAG:").append(TAG).append("\t\n");
	// ex.printStackTrace(printWriter);
	//
	// Throwable cause = ex.getCause();
	// while (cause != null) {
	// cause.printStackTrace(printWriter);
	// cause = cause.getCause();
	// }
	// printWriter.close();
	// } catch (Exception e) {
	// }
	// }

	// /**
	// * 获得日志文件名称
	// *
	// * @return
	// */
	// private static String getLogFileName() {
	// long timestamp = System.currentTimeMillis();
	// // StringUtil.getDateTime() + " " +
	// String fileName = timestamp + ".txt";
	// String path = "";
	// CoreApplication app = CoreApplication.getInstance();
	// if (app != null) {
	// path = app.getLogDirName() + "/";// + "/" + fileName
	// }
	// path = Environment.getExternalStorageDirectory().getPath() + "/" + path;
	//
	// //
	// File dir = new File(path);
	// if (!dir.exists()) {
	// dir.mkdir();
	// }
	// return path + fileName;
	// }
}
